﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
using System.Collections;
using System.Web.UI.WebControls;
using blackhouse.Web.Controls;
using blackhouse.Web.FasterFiles;
using System.ComponentModel;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.Web.UI {

	[ParseChildren(true, "Items")]
	[Browsable(true)]
	public sealed class SmoothGallery : Control {

		#region Fields

		private Panel pnlHolder;

		#endregion

		#region Constructors

		public SmoothGallery() {
			this.Items = new ArrayList();
		}

		#endregion

		#region Properties

		public ArrayList Items { get; set; }

		#endregion

		#region Event Handlers

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			JQueryLibraries jql = new JQueryLibraries();
			jql.IncludeFileBasedOnJQuery("blackhouse.Web.UI.SmoothGallery.mootools-1.2.1-core-yc.js,blackhouse.Web");
			jql.IncludeFileBasedOnJQuery("blackhouse.Web.UI.SmoothGallery.mootools-1.2-more.js,blackhouse.Web");
			jql.IncludeFileBasedOnJQuery("blackhouse.Web.UI.SmoothGallery.jd.gallery.js,blackhouse.Web");

			FasterFilesProvider fasterFiles = new FasterFilesProvider();
			fasterFiles.AddCssFile(typeof(SmoothGallery), "blackhouse.Web.UI.SmoothGallery.layout.css");
			fasterFiles.AddCssFile(typeof(SmoothGallery), "blackhouse.Web.UI.SmoothGallery.jd.gallery.css");

		}

		protected override void OnPreRender(EventArgs e) {
			base.OnPreRender(e);

			this.addStartScript();
		}

		#endregion

		#region Methods

		private void addStartScript() {
			string script = "<script type=\"text/javascript\">" + string.Format(@"
function startGallery() {1}
var myGallery = new gallery($('#{0}'), {1}
timed: false
{2});
{2}

</script> ", this.pnlHolder.ClientID, "{", "}");
			if(!this.Page.ClientScript.IsClientScriptBlockRegistered("sgallery"))
				this.Page.ClientScript.RegisterClientScriptBlock(typeof(SmoothGallery), "sgallery", script, false);
		}

		protected override void CreateChildControls() {
			pnlHolder = new Panel();
			this.Controls.Add(pnlHolder);
			foreach(SmoothGalleryItem item in this.Items) {
				if(item == null) continue;

				Panel pnlImageHolder = new Panel();
				this.pnlHolder.Controls.Add(pnlImageHolder);
				pnlImageHolder.CssClass = "imageElement";
				Literal title = new Literal();
				pnlImageHolder.Controls.Add(title);
				title.Text = string.Format("<h3>{0}</h3>", item.Title);
				Literal description = new Literal();
				pnlImageHolder.Controls.Add(description);
				description.Text = string.Format("<p>{0}</p>", item.Description);
				Literal openImage = new Literal();
				pnlImageHolder.Controls.Add(openImage);
				openImage.Text = "<a href=\"#\" title=\"open image\" class=\"open\"></a>";
				BorderlessImage imgFull = new BorderlessImage();
				pnlImageHolder.Controls.Add(imgFull);
				imgFull.ImageUrl = item.FullImageUrl;
				imgFull.CssClass = "full";
				BorderlessImage  imgThumb = new BorderlessImage();
				pnlImageHolder.Controls.Add(imgThumb);
				imgThumb.ImageUrl = item.ThumbailImageUrl;
				imgThumb.CssClass = "thumbnail";
			}
		}

		#endregion

	}
}